package com.gin.kotlin.gf2gamecommunity.composable

import androidx.compose.foundation.layout.Row
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import com.gin.kotlin.enums.ComplaintReason
import com.gin.kotlin.gf2gamecommunity.base.composable.ConfirmDialog
import com.gin.kotlin.gf2gamecommunity.base.composable.DropdownMenuSelector
import com.gin.kotlin.gf2gamecommunity.viewmodel.CommentListViewModel
import com.gin.kotlin.gf2gamecommunity.viewmodel.TopicListViewModel
import com.gin.kotlin.response.comment.CommentInfo

/**
 * 举报弹窗
 * @param topicId 主题ID
 * @param commentInfo 被举报回复
 * @param topicListViewModel TopicListViewModel
 * @param commentListViewModel CommentListViewModel
 * @param onDismissRequest 关闭弹窗的方法
 * @param modifier Modifier
 * @param onSuccess 举报成功回调
 */
@Composable
fun ComplaintDialog(
    topicId: Int,
    commentInfo: CommentInfo,
    topicListViewModel: TopicListViewModel = viewModel(),
    commentListViewModel: CommentListViewModel = viewModel(),
    onDismissRequest: () -> Unit,
    modifier: Modifier = Modifier,
    onSuccess: () -> Unit = {},
) {
    var reason by remember { mutableStateOf(ComplaintReason.ADVERTISEMENT_GARBAGE) }

    ConfirmDialog(onDismissRequest = onDismissRequest, onConfirmation = {
        commentInfo.apply {
            if (commentId == 0) {
                topicListViewModel.complaint(topicId, reason) {
                    // 请求成功后关闭窗口，并调用回调
                    onDismissRequest()
                    onSuccess()
                }
            } else {
                commentListViewModel.complaint(topicId, reason, commentId) {
                    // 请求成功后关闭窗口，并调用回调
                    onDismissRequest()
                    onSuccess()
                }
            }
        }
    }) {
        Row {
            Text("请选择举报理由: ")
            DropdownMenuSelector(list = ComplaintReason.entries, initValue = ComplaintReason.ADVERTISEMENT_GARBAGE, onSelected = {
                reason = it
            }, toText = { it?.des ?: "未选择理由" })
        }
    }

}